<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layuimini-main">

        <form class="layui-form" action="">
            <input type="hidden" name="id" value="{$id}">
            <div class="layui-form-item">
                <label class="layui-form-label">类型名称</label>
                <div class="layui-input-block">
                    <input type="text" name="qtype_title" value="{$qtype.qtype_title}" lay-verify="qtype_title" autocomplete="off" placeholder="请输入类型名称" class="layui-input iwidth">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型图标</label>
                <input type="hidden" id="imgurlHidden" name="qtype_imgurl" value="{$qtype.qtype_imgurl}">
                <div class="layui-input-inline uploadHeadImage">
                    <div class="layui-upload-drag" id="headImg">
                        <i class="layui-icon"></i>
                        <p>点击上传图片，或将图片拖拽到此处</p>
                    </div>
                </div>
                <div class="" style="display: flex">
                    <div class="layui-upload-list" style="margin-top: 5px;">
                        <img class="layui-upload-img headImage" style="height: 100px;" src="/upload/{$qtype.qtype_imgurl}" id="imgurl">
                        <p id="demoText"></p>
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>为保证图片质量，缩略图请尽量选择200*200的方形图片,图片大小不要超过500KB,一般在30KB-100KB之间
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即修改</button>
                </div>
            </div>
        </form>

    </div>
</div>

{include file="public/footer" /}

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate','upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , upload = layui.upload
            , $ = layui.jquery;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            qtype_title: function (value) {
                if (value == '') {
                    return '类型不能为空';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(add)', function (data) {
            $.ajax({
                url: '/admin/apply/qtype_write?active=ajax',
                type: 'post',
                dataType: 'json',
                data: data.field,
                success: function (res) {
                    if (res.status == true) {    // 获得frame索引
                        parent.layer.msg("操作成功!", {icon:1,time: 1000}, function () {
                            //重新加载父页面
                            parent.location.reload();
                        });
                        return;
                    } else{
                        layer.msg(res.msg,{icon:5,time:1000});

                    }
                },
                error: function () {
                    layer.msg("报错了");
                }
            });
            return false;
            /*layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })*/
        });

        //单图片拖拽上传
        let uploadInst = upload.render({
            elem: '#headImg',
            url: '/admin/industry/Doupload',
            size: 500,
            field: 'imgurl',
            accept: 'images',
            acceptMime: 'image/*',
            before: function(obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result) {
                    $('#imgurl').attr('src', result); //图片链接（base64）;
                });
                this.data = {
                    'mark': 'img',
                    'field': 'imgurl',
                    'oldSrc': $("#imgurlHidden").val()
                };
            },
            done: function(res) {
                console.log( $("#imgurlHidden").val())
                //如果上传失败
                if (res.status == false) {
                    return layer.msg('上传失败');
                }
                //上传成功
                //打印后台传回的地址: 把地址放入一个隐藏的input中, 和表单一起提交到后台, 此处略..
                // window.parent.uploadHeadImage(res.src);
                $("#imgurlHidden").val(res.src);
                let demoText = $('#demoText');
                demoText.html('<span style="color: #8f8f8f;">上传成功!!!</span>');
            },
            error: function() {
                //演示失败状态，并实现重传
                let demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function() {
                    uploadInst.upload();
                });
            },

        });


    });
</script>
</body>
</html>